{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Tensorflow_Dataset_Example.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "form",
        "colab": {},
        "colab_type": "code",
        "id": "S7sxJvD18-3n"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tCVAy3FaiZ5F",
        "colab_type": "text"
      },
      "source": [
        "##Tensorflow Datasets: Slicing\n",
        "\n",
        "_Notebook orignially contributed by: [Abhinav Prakash](https://github.com/abhinavsp0730)_\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/examples/blob/master/community/en/Tensorflow_Datasets_Example.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/examples/blob/master/community/en/Tensorflow_Datasets_Example.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "By0bXoK6fX5n",
        "colab_type": "text"
      },
      "source": [
        "This colab notebook contains a guide to use the [TensorFlow Datasets](https://tensorflow.org/datasets) slicing API. We're going to see how easily we can load a dataset and perform the split, slice and merge operations. \n",
        "\n",
        "**Note:** Previously we're having two APIs to load the data i.e Legacy API and \n",
        "S3 API. But now the legacy split is deprecated and does not work anymore."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xOqtN855i15k",
        "colab_type": "text"
      },
      "source": [
        "First let's import Tensorflow and Tensorflow datasets.\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "v_FLi_vkiExb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "try:\n",
        "  # %tensorflow_version only works in Colab.\n",
        "  %tensorflow_version 2.x\n",
        "except:\n",
        "  pass\n",
        "  "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pLBrzWSNpZjM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import tensorflow as tf\n",
        "import tensorflow_datasets as tfds"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AW9RsblrpW0l",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(tf.__version__)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HGA9YX_EjH9f",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "> Currently, there are all popular datasets present in the Tensorflow Dataset. \n",
        "We can get the list of all the datasets ``` list_builders``` method. By calling this method, it returns the list of all the datasets available in Tensorflow Datasets. In this notebook, I'm going to use the mnist dataset for doing the operations.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2CTaQPQeBoQH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(tfds.list_builders())\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M_DzkC4fCaI9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# To check the mnist dataset present or not.\n",
        "for mnist in tfds.list_builders():\n",
        "  if(mnist==\"mnist\"):\n",
        "    print(\"mnist dataset is present in tensorflow datasets\")\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GP1cFFK7mNI7",
        "colab_type": "text"
      },
      "source": [
        "Mnist dataset comes with 70000 records and it's having predefined splits, train split having 60000 records and the test split is having 10000 records. We can load this method by ```load``` method. We can pass the dataset name as a string to download it in the ```tfds.load``` . And we pass the list of the splits that we need to tfds.load in the split parameter."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PV6vnuoIDWQr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_ds, test_ds = tfds.load(\"mnist\", split=[\"train\",\"test\"])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3uPBtd9LDWbM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(\"Number of Records in training set: {:,}\".format(len(list(train_ds))))\n",
        "print(\"Number of Records in test set: {:,}\".format(len(list(test_ds))))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uPHG2JmbFin1",
        "colab_type": "text"
      },
      "source": [
        "Slicing is the operation by which can construct a dataset that contains the required specific number of records from the original datasets. \n",
        "By using S3 API, We can do slicing operations in two ways:\n",
        "* By specifying the slice that we want with the familiar colon syntax from Python\n",
        "encoded in a string literal.\n",
        "* By passing the required percantage(%) in the split parameter.\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XjlR20BlDW80",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# By using the colon syntax\n",
        "# From record 10000 (included) to record 40000 (excluded) of `train` split.\n",
        "dataset_1 = tfds.load('mnist', split='train[1000:4000]')\n",
        "print(\"Number of Records in dataset_1 : {:,}\".format(len(list(dataset_1))))\n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m_eXGfjYFi_6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# The first 20 percentage from `train` split by passing required percentage as a parameter.\n",
        "dataset_2 = tfds.load('mnist', split='train[:20%]')\n",
        "print(\"Number of Records in dataset_2 : {:,}\".format(len(list(dataset_2))))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VggV8MBtZuW8",
        "colab_type": "text"
      },
      "source": [
        "*Merging* is the operation by which we can construct a dataset having required \n",
        "records from another datasets."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wFJGcGnPzs50",
        "colab_type": "text"
      },
      "source": [
        "We can merge the train and test datset using S3 API. The resulting dataset after merging contains all the records from the train and the test splits.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rsKBDk3UYRX2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Full dataset contains 70000 records(60000(train)+ 10000(test)).\n",
        "full_dataset = tfds.load('mnist', split='train+test')\n",
        "print(\"Number of Records in full_dataset : {:,}\".format(len(list(full_dataset))))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xO383RfTagjx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# dataset_3 contains first 20%  from 'train' split and last 30% from the 'test' split.\n",
        "dataset_3 = tfds.load('mnist', split='train[:20%]+test[-70%:]')\n",
        "print(\"Number of Records in dataset_3 : {:,}\".format(len(list(dataset_3))))\n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-qQJWNJ32PsL",
        "colab_type": "text"
      },
      "source": [
        "This is how we can easily load, slice and merge the dataset using Tensorflow Dataset. We can also add the new dataset in Tesnorflow Dataset for helping the community. So that people like us can't spend much time in loading the datasets.\n",
        "You can see the Documentation here for adding the new datasets\n",
        ": [Adding a Dataset](https://www.tensorflow.org/datasets/add_dataset) \n",
        "And then send a pull request in Github here: [Tensorflow/dataset](https://github.com/tensorflow/datasets)"
      ]
    }
  ]
}
